// 在组件实例中使用方便展示，在编辑区以及预览区使用不到
var timeDate = "map" + new Date().getTime();
var mapGeoJson;
// 初始化属性
async function defineComponentProps() {
  const componentProps = {
    mapDataCode: "00028",
    mapInfo: {
      dataType: 1,
      name: "全国",
      mapName: "全国",
      url: "",
      mapCode: "100000",
    },
  };
  var points = [
    { value: [118.8062, 31.9208], itemStyle: { color: "#4ab2e5" } },
    { value: [127.9688, 45.368], itemStyle: { color: "#4fb6d2" } },
    { value: [110.3467, 41.4899], itemStyle: { color: "#52b9c7" } },
    { value: [125.8154, 44.2584], itemStyle: { color: "#5abead" } },
    { value: [116.4551, 40.2539], itemStyle: { color: "#f34e2b" } },
    { value: [123.1238, 42.1216], itemStyle: { color: "#f56321" } },
    { value: [114.4995, 38.1006], itemStyle: { color: "#f56f1c" } },
    { value: [117.4219, 39.4189], itemStyle: { color: "#f58414" } },
    { value: [112.3352, 37.9413], itemStyle: { color: "#f58f0e" } },
    { value: [109.1162, 34.2004], itemStyle: { color: "#f5a305" } },
    { value: [103.5901, 36.3043], itemStyle: { color: "#e7ab0b" } },
    { value: [106.3586, 38.1775], itemStyle: { color: "#dfae10" } },
    { value: [101.4038, 36.8207], itemStyle: { color: "#d5b314" } },
    { value: [103.9526, 30.7617], itemStyle: { color: "#c1bb1f" } },
    { value: [108.384366, 30.439702], itemStyle: { color: "#b9be23" } },
    { value: [113.0823, 28.2568], itemStyle: { color: "#a6c62c" } },
    { value: [102.9199, 25.46639], itemStyle: { color: "#96cc34" } },
    { value: [119.4543, 25.9222] },
  ];
  let option = {
    backgroundColor: "",
    geo: {
      map: timeDate,
      aspectScale: 0.75, //长宽比
      tooltip: {
        show: true,
      },
      zoom: 1.1,
      zIndex: 99,
      roam: true,
      itemStyle: {
        normal: {
          borderColor: "rgb(147, 235, 248)",
          borderWidth: 1,
          areaColor: {
            type: "radial",
            x: 0.5,
            y: 0.5,
            r: 0.8,
            colorStops: [
              {
                offset: 0,
                color: "rgba(9,19,44,1)", // 0% 处的颜色
              },
              {
                offset: 1,
                color: "rgba(39,77,104,1)", // 100% 处的颜色
              },
            ],
            globalCoord: true, // 缺省为 false
          },
        },
        emphasis: {
          areaColor: "rgb(46,229,206)",
          //    shadowColor: 'rgb(12,25,50)',
          borderWidth: 0.1,
        },
      },
      label: {
        normal: {
          show: true,
          textStyle: {
            fontSize: 16,
            color: "rgba(29,233,182,1)",
          },
        },
        emphasis: {
          textStyle: {
            color: "rgb(183,185,14)",
          },
        },
      },
      regions: [
        {
          name: "南海诸岛",
          itemStyle: {
            areaColor: "rgba(0, 10, 52, 1)",

            borderColor: "rgba(0, 10, 52, 1)",
            normal: {
              // opacity: 0,
              label: {
                show: false,
                color: "rgba(0,156,201,1)",
              },
            },
          },
        },
      ],
    },
    series: [
      {
        type: "effectScatter",
        coordinateSystem: "geo",
        showEffectOn: "render",
        zlevel: 1,
        rippleEffect: {
          period: 15,
          scale: 4,
          brushType: "fill",
        },
        hoverAnimation: true,
        label: {
          normal: {
            formatter: "{b}",
            position: "right",
            offset: [15, 0],
            color: "rgba(29,233,182,1)",
            show: true,
          },
        },
        itemStyle: {
          normal: {
            color: "rgba(29,233,182,1)",
            shadowBlur: 10,
            shadowColor: "#333",
          },
        },
        symbolSize: 12,
        data: points,
      }, //地图线的动画效果
      {
        type: "lines",
        zlevel: 2,
        effect: {
          show: true,
          period: 4, //箭头指向速度，值越小速度越快
          trailLength: 0.4, //特效尾迹长度[0,1]值越大，尾迹越长重
          symbol: "arrow", //箭头图标
          symbolSize: 7, //图标大小
        },
        lineStyle: {
          normal: {
            color: "#1DE9B6",
            width: 1, //线条宽度
            opacity: 0.1, //尾迹线条透明度
            curveness: 0.3, //尾迹线条曲直度
          },
        },
        data: [
          {
            coords: [
              [118.8062, 31.9208],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#4ab2e5" },
          },
          {
            coords: [
              [127.9688, 45.368],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#4fb6d2" },
          },
          {
            coords: [
              [110.3467, 41.4899],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#52b9c7" },
          },
          {
            coords: [
              [125.8154, 44.2584],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#5abead" },
          },
          {
            coords: [
              [116.4551, 40.2539],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#f34e2b" },
          },
          {
            coords: [
              [123.1238, 42.1216],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#f56321" },
          },
          {
            coords: [
              [114.4995, 38.1006],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#f56f1c" },
          },
          {
            coords: [
              [117.4219, 39.4189],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#f58414" },
          },
          {
            coords: [
              [112.3352, 37.9413],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#f58f0e" },
          },
          {
            coords: [
              [109.1162, 34.2004],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#f5a305" },
          },
          {
            coords: [
              [103.5901, 36.3043],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#e7ab0b" },
          },
          {
            coords: [
              [106.3586, 38.1775],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#dfae10" },
          },
          {
            coords: [
              [101.4038, 36.8207],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#d5b314" },
          },
          {
            coords: [
              [103.9526, 30.7617],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#c1bb1f" },
          },
          {
            coords: [
              [108.384366, 30.439702],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#b9be23" },
          },
          {
            coords: [
              [113.0823, 28.2568],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#a6c62c" },
          },
          {
            coords: [
              [102.9199, 25.46639],
              [119.4543, 25.9222],
            ],
            lineStyle: { color: "#96cc34" },
          },
        ],
      },
    ],
  };
  componentProps.options = option;
  // 返回组件属性
  return componentProps;
}

// 组件创建前
async function defineComponentBeforeCreated({ VueElement }) {
  // console.log("map001 defineComponentBeforeCreated")
  try {
    if (VueElement) {
      // 兼容2.5的写法，已经无全局变量需要调整处理
      if (VueElement.component.props && VueElement.component.props.mapInfo) {
        // 刷新后读取最新的数据
        window[`${VueElement.id}mapGepJson`] =
          VueElement.component.props.mapInfo.json;
      } else {
        // 若无切换图层则使用默认的数据
        // mapUrl地址会在SDA2.5中进行默认转换
        let mapUrl =
          BASE_URL +
          "/adminapp/public/assets/elfinder/files/json/geo/中国.json";
        let getMapJson = function () {
          return fetch(mapUrl).then((res) => res.json());
        };
        window[`${VueElement.id}mapGepJson`] = await getMapJson();
      }
      echarts.registerMap(VueElement.id, window[`${VueElement.id}mapGepJson`]);
    } else {
      // 兼容在组件库展示效果的写法
      let mapUrl =
        BASE_URL + "/adminapp/public/assets/elfinder/files/json/geo/中国.json";
      let getMapJson = function () {
        return fetch(mapUrl).then((res) => res.json());
      };
      let mapGeoJson = await getMapJson();
      echarts.registerMap(VueElement ? VueElement.id : timeDate, mapGeoJson);
    }
  } catch (error) {
    console.log("error1", error);
  }
}

// 组件创建后
async function defineComponentCreated({ VueElement, VueInstance }) {
  // console.log("map001 defineComponentCreated")
  // 由于在defineComponentBeforeCreated生命周期中，无法获取VueInstance，所以在此生命周期重置地图唯一标识
   if (VueElement && VueInstance) {
     VueInstance.options.geo.map = VueElement.id;
   }
}

async function updateMap(data, VueInstance) {
  let mapGeoJson = data;
  echarts.registerMap(VueInstance.options.series[0].map, mapGeoJson);
  mapDataRender = mapGeoJson.features.map((item) => {
    return {
      value: (Math.random() * 1000).toFixed(2),
      name: item.properties.name,
    };
  });
  if (VueInstance) {
    VueInstance.options.series[0].data = mapDataRender;
    VueInstance.updateOptions(VueInstance.options);
  }
}

async function handleDataCallback({ ResourceData, VueInstance }) {
  // console.log("map001 handleDataCallback")
  if (VueInstance) {
    updateMap(ResourceData, VueInstance);
  }
}
